package com.zelin.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zelin.dao.SpecificationDao;
import com.zelin.entity.SpecificationEntity;
import com.zelin.entity.SpecificationOptionEntity;
import com.zelin.entity.group.Specification;

import com.zelin.sellergoods.service.SpecificationOptionService;
import com.zelin.sellergoods.service.SpecificationService;
import com.zelin.utils.PageUtils;
import com.zelin.utils.Query;
import javafx.scene.shape.ClosePath;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



@Service
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {


    @Autowired
    private SpecificationOptionService optionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(SpecificationEntity entity) {
        return false;
    }

    /**
     * 功能: 添加规格及期规格选项
     * 参数:
     * 返回值: void
     * 时间: 2021/7/26 15:52
     */
    @Override
    public void add(Specification specification) {
        //1. 添加规格对象
        SpecificationEntity spec = specification.getSpec();
        baseMapper.insert(spec);
        System.out.println("spec = " + spec);

        //2. 添加规格选项
        List<SpecificationOptionEntity> options = specification.getOptions();
        for (SpecificationOptionEntity option : options) {
            //2.1 为规格选项指定外键
            option.setSpecId(spec.getId());
            //2.2 添加规格选项
            optionService.save(option);
        }

    }

    /**
     * 功能: 根据id查询规格及其规格选项
     * 参数:
     * 返回值: com.zelin.entity.group.Specification
     * 时间: 2021/7/26 16:17
     */
    @Override
    public Specification findById(Long id) {
        //1. 根据规格id查询规格
        SpecificationEntity specificationEntity = baseMapper.selectById(id);
        //2. 根据规格id在规格选项表中查询出此规格对应的规格选项列表
        //2.1 定义查询条件
        QueryWrapper<SpecificationOptionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id",id);
        //2.2 根据条件查询出规格选项列表
        List<SpecificationOptionEntity> list = optionService.list(queryWrapper);
        //3. 定义规格的组合对象
        Specification specification = new Specification();
        specification.setSpec(specificationEntity);
        specification.setOptions(list);
        return specification;
    }

    /**
     * 功能: 修改规格及其规格选项
     * 参数:
     * 返回值: void
     * 时间: 2021/7/26 16:27
     * 注意：
     *   一对多及多对多修改：
     *   ① 根据外键删除多的一方表中的所有数据
     *   ② 再添加到多的一方
     *   ③ 一方直接修改
     */
    @Override
    public void update2(Specification specification) {
        //1. 修改规格表(根据id修改规格表)
        this.updateById(specification.getSpec());

        //2. 根据规格id在多的表中删除数据(根据外键删除多的一方)
        QueryWrapper<SpecificationOptionEntity> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("spec_id",specification.getSpec().getId());
        optionService.remove(deleteWrapper);

        //3. 再添加新的数据到多的表中
        //3.1 得到规格选项的值
        List<SpecificationOptionEntity> options = specification.getOptions();
        for (SpecificationOptionEntity option : options) {
            //3.2 设置关联的外键
            option.setSpecId(specification.getSpec().getId());
            //3.2 添加规格选项到规格选项表中
            optionService.save(option);
        }

    }

    /**
     * 功能: 根据传入的多个规格id，删除规格及规格选项
     * 参数:
     * 返回值: void
     * 时间: 2021/7/26 16:48
     */
    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            //1. 删除主表规格表
            this.removeById(id);

            //2. 规格选项表
            QueryWrapper<SpecificationOptionEntity> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.eq("spec_id",id);
            optionService.remove(deleteWrapper);
        }
    }

}